function QuestionManage() {
    var _this = this;
    var page;
    _this.obj = null;
    _this.templateArr = [];
    _this.page_size = 10;

    // init
    _this.init = function(_page) {
        page = _page;
        _this.updateQuestionCount();
        _this.initTable();
        $("#varContent").keydown(function () {
            if(event.keyCode == "13"){
                _this.initTable();
                return false;
            }
        })

    };
    // 更新课程下的题库数量
    _this.updateQuestionCount = function () {
            $.ajax({
                url: appPath+"/admin/exCourseSelf/updateQuestionCount",
                data: {
                    "courseId":$("#courseId").val()
                },
                dataType: "json",
                async: true,
                type: "POST",
                success: function (data) {

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(textStatus,{icon:2});
                }
            });

    };


    _this.initTable = function() {
        var list_url = appPath + "/admin/questionSelf/list";
        page.init("question_self_form", list_url, "question_self_list", "question_self_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function(dataList){
            _this.obj = dataList;
            if(dataList == null){
                return;
            }
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                inner += '<tr >';
                inner += '<td width="30">' + (i+1) + '</td>';
                inner += '<td width="40"><input type=\"checkbox\" value="' + item['intId'] + '" name="courseBox" data-id="' + item['intId'] + '"></td>';
                inner += '<td  width="80">'+ item['varNo'] + '</td>';
                inner += '<td width="50">'+ item['chrType'] + '</td>';
                inner += '<td width="250">'+ item['varContent'] + '</td>';
                if(item['chrType'] == "单选" ||　item['chrType'] == "多选" ||　item['chrType'] == "判断"){
                    inner += '<td width="100">'+ tool2tip(item['varAnswer']) + '</td>';
                }else {
                    inner += '<td width="100">'+ tool2tip(item['varAnswerDesc']) + '</td>';
                }
                inner += '<td width="70">'+ item['datCreateDate'].substring(0,10) + '</td>';
                inner += '<td width="200">';
                inner += '<a href="javascript:question_manage.info(\''+ item['intId'] +'\');" class="a a-view">详情</a> ';
                inner += '<a href="javascript:question_manage.edit(\''+ item['intId'] +'\');" class="a a-info">编辑</a> ';
                inner += '<a href="javascript:question_manage.delete_question(\''+ item['intId'] +'\');" class="a a-close">删除</a> ';
                inner += '</td></tr>';
            }
            return inner;
        }
    };


    //关闭对话框
    _this.closeDialog =function () {
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    }

    //批量删除
    _this.delete_questions=function () {
        var questionIds=[];
        $("#question_self_list input:checked").each(function (i,e) {
            questionIds.push($(this).attr("data-id"));
        });
        _this.delete(questionIds);
    }

    //单个删除
    _this.delete_question=function (questionId) {
        var questionIds = [questionId];
        _this.delete(questionIds);
    }


    //删除
    _this.delete=function (questionIds) {
        layer.confirm('确定要删除选择的数据吗？', {btn: ['是','否']}, function(index){
            layer.close(index);
            $.ajax({
                url: appPath+"/admin/questionSelf/delete",
                data: {
                    "companyId":$("#companyId").val(),
                    "courseId":$("#courseId").val(),
                    "chrStatus":"",
                    "questionIds":questionIds.join(",")
                },
                dataType: "json",
                async: true,
                type: "POST",
                success: function (data) {
                    var code = data.code;
                    var count = data.result;
                    if (code == "10000" ) {
                        if("0" == count){
                            layer.msg("删除成功",{icon:1});
                        }else{
                            var tipMsg = count +"个课程删除失败!";
                            tipMsg += "\n 该课程下可能有附件或试题，如要删除请先删除课件或试题，若该课程已被项目引用，不允许删除" ;
                            layer.msg(tipMsg,{icon:5}) ;
                        }

                        // 触发查询事件
                        $("#question_content_search").trigger("click");
                    } else {
                        var tipMsg = "删除失败!";
                        var error = eval(data).errorMsg;
                        if(error != null && error != "") {
                            tipMsg += "\n" + error;
                        }
                        layer.msg(tipMsg,{icon:2}) ;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(textStatus,{icon:2});
                }
            });
        },function(){

        });
    };


    _this.ajax = function(url, param, type){
        var result;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function(data){
                result = data.result;
            }
        });
        return result;
    };

    _this.add = function(){
        var companyId = $("#companyId").val();
        var courseId = $("#courseId").val();
        var chrStatus = $("#chrStatus").val();
        var url = appPath + '/admin/questionSelf/add?companyId='+companyId+'&courseId='+courseId+'&chrStatus='+chrStatus;
        window.open(url,"_blank");
    };

    _this.info = function(id){
        var url = appPath + '/admin/questionSelf/detail?id='+id+'&isUpdate=0';
        window.open(url,"_blank");
    };

    _this.edit = function(id){
        var chrStatus = $("#chrStatus").val();
        var courseId = $("#courseId").val();
        var url = appPath + '/admin/questionSelf/detail?id='+id+'&isUpdate=1&courseId='+courseId+'&chrStatus='+chrStatus;
        window.open(url,"_blank");
    };


    _this.objective_question_import = function(){
        var companyId = $("#companyId").val();
        var courseId = $("#courseId").val();
        var url = appPath + '/admin/questionSelf/objective_import?companyId='+companyId+'&courseId='+courseId;
        window.open(url,"_blank");
    }

    _this.subjective_question_import = function(){
        var companyId = $("#companyId").val();
        var courseId = $("#courseId").val();
        var url = appPath + '/admin/questionSelf/subjective_import?companyId='+companyId+'&courseId='+courseId;
        window.open(url,"_blank");
    }

    _this.forback = function(){
        window.opener.course_self_list.search();
        window.close();
    }

    //查询全部
    _this.searchAll=function(){
        $("#varContent").val("");
        _this.initTable();
    };

    _this.search=function(){
        _this.initTable();
    };

    //全选
    $("#checkAll").click(function () {
        _this.questionCheckAll();
    })

    //试题全选
    _this.questionCheckAll=function(){
        var flag=$("#checkAll").prop("checked");
        $('#question_self_list input[type="checkbox"]').prop("checked",flag);
    }
}

var question_manage = new QuestionManage();


//搜索高亮
function getFontCss(treeId, treeNode){
    return (!!treeNode.highlight)?{'color': "#A60000", 'font-weight':"bold"}:{'color': "#333", 'font-weight':"normal"};
}

var tool2tip = function (data) {
    if(null == data || "" == data){
        return "";
    }
    var data1 = data.length >= 15 ? data.substring(0,15) + "...": data;
    var html = '<a href="javascript:;" class="dropdown-toggle" data-toggle="tooltip" title="'+ data +'">'+ data1 +'</a>';
    return html;
}